﻿using System;
using System.Web.UI;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PrintContract : BasePage<PrintContractViewModel>
    {

        /// <summary>
        /// Gets the execution ID.
        /// </summary>
        private string ExecutionID
        {
            get { return Request.QueryString["ExecutionID"]; }
        }

        /// <summary>
        /// Gets the loan ID.
        /// </summary>
        private string LoanID
        {
            get { return Request.QueryString["LoanID"]; }
        }

        /// <summary>
        /// Gets the execution date.
        /// </summary>
        private string executionDate
        {
            get { return Request.QueryString["executionDate"]; }
        }


        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ILoanExecution executionObject = null;
                ICustomerLoan customerLoan = null;
                if (!ExecutionID.IsNullOrEmpty())
                {
                    executionObject = this.ViewModel.GetLoanExecutionByID(ExecutionID.ToGuid());
                }
                if (!LoanID.IsNullOrEmpty())
                {
                    DateTime exeDate = DateTime.Now.Date;
                    if (executionDate != null)
                        exeDate = DateTime.Parse(executionDate);

                    executionObject = this.ViewModel.GenerateExecution(LoanID.ToGuid(), exeDate, exeDate
                        , "***guarantor", "****ssn");
                    customerLoan = this.ViewModel.GetLoanObject(LoanID.ToGuid());
                }
                if (executionObject != null)
                {
                    if (executionObject.CustomerLoan != null)
                    {
                        txtCustomerName.Text = executionObject.CustomerLoan.CustomerObj.CustomerName;
                        txtCustomerBirthDate.Text = string.Format("{0:dd/MM/yyyy}", executionObject.CustomerLoan.CustomerObj.Birthdate);
                        txtCustomerBirthPlace.Text = executionObject.CustomerLoan.CustomerObj.BirthLocationObj.CenterName;
                        txtCustomerSSN.Text = executionObject.CustomerLoan.CustomerObj.SSN;
                        txtCustomerSNNIssuePlace.Text = executionObject.CustomerLoan.CustomerObj.IssuePlaceObj.IssuePlaceName;
                        txtCustomerSNNGovernorate.Text = executionObject.CustomerLoan.CustomerObj.IssuePlaceObj.CenterObj.GovernorateObj.GovernorateName;
                        txtSNNCreationDate.Text = string.Format("{0:dd/MM/yyyy}", executionObject.CustomerLoan.CustomerObj.SSNCreationDate);
                        txtLoanType.Text = executionObject.CustomerLoan.LoanTypeObj.Name;
                    }
                    else
                    {
                        if (customerLoan != null)
                        {
                            txtCustomerName.Text = customerLoan.CustomerObj.CustomerName;
                            txtCustomerBirthDate.Text = string.Format("{0:dd/MM/yyyy}", customerLoan.CustomerObj.Birthdate);
                            txtCustomerBirthPlace.Text = customerLoan.CustomerObj.BirthLocationObj.CenterName;
                            txtCustomerSSN.Text = customerLoan.CustomerObj.SSN;
                            txtCustomerSNNIssuePlace.Text = customerLoan.CustomerObj.IssuePlaceObj.IssuePlaceName;
                            txtCustomerSNNGovernorate.Text = customerLoan.CustomerObj.IssuePlaceObj.CenterObj.GovernorateObj.GovernorateName;
                            txtSNNCreationDate.Text = string.Format("{0:dd/MM/yyyy}", customerLoan.CustomerObj.SSNCreationDate);
                            txtLoanType.Text = customerLoan.LoanTypeObj.Name;
                        }
                    }
                    txtLoanAmount.Text = executionObject.LoanAmount.ToString();
                    txtLoanExecutionDate.Text = string.Format("{0:dd/MM/yyyy}", executionObject.ExecutionDate);
                    txtLoanPaymentFreeTime.Text = (executionObject.LoanPeriod - 1).ToString();
                    txtLoanPaymentPeriod.Text = executionObject.LoanPeriod.ToString();
                    txtLoanPaymentRound.Text = "شهرية";
                    txtLoanPeriod.Text = executionObject.LoanPeriod.ToString();
                    txtLoanAmount2.Text = Math.Round(executionObject.LoanAmount / executionObject.LoanPeriod, 2).ToString();



                }
                txtCurrentDate.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Now.Date);
                txtCommitteePlace.Text = this.ViewModel.GetCurrentCommittee().CommitteeName;
            }
        }
    }
}
